package com.dbh.company;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.dbh.company.buildbook.bean.ConfigParam;
import com.dbh.company.buildbook.bean.ConfigVO;
import com.dbh.company.buildbook.bean.RespBase;
import com.dbh.company.util.S3Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author dongbanghui
 * @create 2023/11/1
 */
public class ComplianceDelete {

    private static final Logger LOGGER = LoggerFactory.getLogger(ComplianceDelete.class);

    /**
     * 收集s3的key
     * @return
     */
    public static RespBase getS3KeyList(Long projectNum) {
        ConfigParam configParam = new ConfigParam();
        configParam.setProjectNum(projectNum);
        String url = "http://pre-mr.ke.com/api/config/getConfigById";
        String body = HttpRequest
                .post(url)
                .body(JSONUtil.toJsonStr(configParam))
                .execute()
                .body();
        RespBase respBase = JSON.parseObject(body, RespBase.class);
        return respBase;
    }

    public static void main(String[] args) throws IOException, InterruptedException {
//        ThreadPoolExecutor threadPoolExecutor = ThreadUtil.newExecutor(200, 200);
//        List<String> list = FileUtil.readLines("D:\\data\\work\\file\\楼书\\楼书1.0\\网信办\\projectNum.txt", "UTF-8");
//        List<List<String>> partition = ListUtil.partition(list, 2);
//        for (List<String> strings : partition) {
//            threadPoolExecutor.execute(() -> {
//                deleteData(strings);
//            });
//        }
//        threadPoolExecutor.shutdown();
        List<String> strList = new ArrayList<>();
        strList.add("606741");
        deleteData(strList);
    }

    private static void deleteData(List<String> list) {
        String bucket = "element-library";
        for (int i = 0; i < list.size(); i++) {
            String item = list.get(i);
            try {
                RespBase s3KeyList = getS3KeyList(Convert.toLong(item));
                ConfigVO data = s3KeyList.getData();
                // 删除s3存储数据
                // 删除封面
                String covertKey = data.getCovertKey();
                if (StrUtil.isNotEmpty(covertKey)) {
                    S3Util.delete(bucket, covertKey);
                }
                // 删除切割图
                List<String> imageKeyList = data.getImageKeyList();
                for (String imageKey : imageKeyList) {
                    if (StrUtil.isNotEmpty(imageKey)) {
                        S3Util.delete(bucket, imageKey);
                    }
                }
                // 删除原始图
                List<String> panoramaFileKeyList = data.getPanoramaFileKeyList();
                for (String panoramaFileKey : panoramaFileKeyList) {
                    if (StrUtil.isNotEmpty(panoramaFileKey)) {
                        S3Util.delete("hdic-3d", panoramaFileKey);
                    }
                }
                // 删除缩略图
                List<String> thumbKeyList = data.getThumbKeyList();
                for (String thumbKey : thumbKeyList) {
                    if (StrUtil.isNotEmpty(thumbKey)) {
                        S3Util.delete(bucket, thumbKey);
                    }
                }
                // 删除全景图切块
                List<String> imageMatrixKeyList = data.getImageMatrixKeyList();
                for (String imageMatrixKey : imageMatrixKeyList) {
                    if (StrUtil.isNotEmpty(imageMatrixKey)) {
                        S3Util.delete(bucket, imageMatrixKey);
                    }
                }
                Console.log("项目所有数据删除成功,项目id为:{}", item);
            } catch (Exception e) {
                LOGGER.error("项目所有数据删除成功,项目id为:{}", item, e);
            }
        }

    }

}
